# -*- coding:utf-8 -*-
from . import project
from flask import render_template,abort,session,request
import logging
from admin_model import Content,List_,Code,Content_admin
from project_zczh import redis_store
import datetime
from project_zczh.tool.common import login_check
@project.route('/detail/<string:key>',methods=['GET'])
@login_check
def detail(key):
    if len(key) == 32:
        try:
            content = Content.query.filter_by(md5=key).all()[0]
        except Exception as f:
            abort(404)
            logging.log('DEBUG', f)
        title = List_.query.filter_by(md5=key).all()[0]
        block_name=Code.query.filter_by(id=title.code_id).all()[0]
        try:
            mark=redis_store.lrange(session.get('user_id'),0,-1)
            if str(title.id) not in mark:
                redis_store.lpush(session.get('user_id'), title.id)
        except Exception as f:
            print f
        exp = datetime.timedelta(4)
        redis_store.expire(session.get('user_id'), exp)

        list_mark = ['toutiaocar', 'toutiaofinance', 'toutiaohot']

        content.site=title.site
        content.url=title.url
        content.title=title.title
        return render_template('detail.html', content=content, title=title,name=block_name.name,list_mark=list_mark)
    else:
        try:
            content=Content_admin.query.filter_by(id=int(key)).all()[0]
        except Exception as f:
            abort(404)
            print f
        title=List_.query.filter_by(content_id=int(key),mark=1).all()[0]

        try:
            mark = redis_store.lrange(session.get('user_id'), 0, -1)
            if str(title.id) not in mark:
                redis_store.lpush(session.get('user_id'),title.id)
        except Exception as f:
            print f
        exp=datetime.timedelta(4)
        redis_store.expire(session.get('user_id'),exp)
        list_mark = ['toutiaocar', 'toutiaofinance', 'toutiaohot']
        return render_template('detail.html',content=content,title=title,name=title.code.name,list_mark=list_mark)
